var zhongchou_collection = db.collection("zhongchou");
(function() {
	// 构造函数
	var _con = function(info, admin) {
        var self = this;
        if (info) {
            /*printData("创建众筹中：", info, admin);
            self.type = "module";*/
            // 自有id
            self.info = info;
            // 创建者
            self.creater = admin || {};
            self.id = "zc_" + Math.random().toString(16).substr(2, 2) + (new Date()).getTime().toString(16);
            // 创建时间
            self.createtime = (new Date()).getTime();
            // 模块名称
            /*self.name = info.name;
            // 创建者
            self.creater = admin || {};
            self.creater_id = admin._id;
            // 图标
            //self.preview_img = info.preview_img;
            // 创建时间
            self.createtime = (new Date()).getTime();*/
        };
    };

    // 导出类
    module.exports = _con;

    // 添加众筹
    _con.prototype.save = co.wrap(function*() {
    	var self = this;
    	if(!self.creater._id){
    		return printError("您不是管理员！");
    	}
    	if(!self.info){
    		return printError("错误！");
    	}
    	if(!self.info.name){
			return printError("标题不能为空！");
    	}
    	if(!self.info.car_id){
			return printError("房车不能为空！");
    	}
    	self.info.number = parseInt(self.info.number);
    	if(!self.info.number||self.info.number<=0){
    		return printError("众筹人数不能为空！");
    	}
    	self.info.totalprice = parseFloat(self.info.totalprice);
    	if(!self.info.totalprice||self.info.totalprice<=0){
			return printError("众筹总价钱不能为空！");
    	}
    	self.info.zcprice = parseFloat(self.info.zcprice);
    	if(!self.info.zcprice||self.info.zcprice<=0){
    		return printError("众筹价钱不能为空！");
    	}
    	//查找房车是否存在。
    	var touringcar = db.collection("touringcar");
    	var arrcar = yield mongo.findOne(touringcar, {
            query: {
                _id: self.info.car_id
            }
        })
        if(!arrcar._id){
			return printError("房车不存在！");
    	}
    	var data = {
    		'_id':self.id,
    		'name':self.info.name,
    		'car_id':self.info.car_id,
    		'number':self.info.number,
    		'totalprice':self.info.totalprice,
    		'zcprice':self.info.zcprice,
    		'createtime':self.createtime
    	};
    	return yield mongo.insertOne(zhongchou_collection,data);
        /*
        return yield mongo.findOne(module_collection, {
            query: {
                name: self.name
            }
        });*/
    });

    // 修改众筹
    _con.prototype.update = co.wrap(function*() {
		var self = this;
    });
}());